package com.biubiushop.CodeTemplate;

import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.*;
import java.util.*;

/**
 * 生成repository文件模板
 * @author chenjia
 */
public class FreemarkerDemo {

    private static final String ENTITY_PATH = "src/main/java/com/biubiushop/entity";
    private static final String TEMPLATE_PATH = "src/main/resources/templates";
    private static final String REPOSITORY_PATH = "src/main/java/com/biubiushop/repository";

    public static void main(String[] args) {
        try {
            //获取repository文件 文件命名格式为ITestRepository
            File resFile = new File(REPOSITORY_PATH);
            File[] resFileList = resFile.listFiles();
            Set<String> resNameSet = new HashSet<>();
            if(resFileList.length > 0){
                for (File res:resFileList) {
                    String resName = res.getName().substring(1,res.getName().indexOf("Repository"));
                    resNameSet.add(resName);
                }
            }

            //获取entity文件 文件命名格式为Test
            File file = new File(ENTITY_PATH);
            File[] entityList = file.listFiles();
            if(entityList.length < 1){
                System.out.println("================目录中没有相应实体=================");
                return;
            }
            Set<String> entityNameSet = new HashSet<>();
            for (File entity:entityList) {
                String entityName = entity.getName().substring(0,entity.getName().indexOf("."));
                entityNameSet.add(entityName);
            }

            //获取未生成的repoitory的entity
            entityNameSet.removeAll(resNameSet);
            if(entityNameSet.isEmpty()){
                System.out.println("================没有未生成repository相应实体=================");
                return;
            }
            if(!entityNameSet.isEmpty()){
                for (String str : entityNameSet) {
                    System.out.println("====================开始生成I"+str+"Repository============================");
                    // step1 创建freeMarker配置实例
                    Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
                    Writer out = null;
                    // step2 获取模版路径
                    configuration.setDirectoryForTemplateLoading(new File(TEMPLATE_PATH));
                    // step3 创建数据模型
                    Map<String, Object> dataMap = new HashMap<String, Object>();
                    dataMap.put("classPath", "com.biubiushop.repository");
                    dataMap.put("entityName", str);
                    // step4 加载模版文件
                    Template template = configuration.getTemplate("repositoryTemplates.ftl");
                    // step5 生成数据
                    File docFile = new File(REPOSITORY_PATH + "\\I" + str+"Repository.java");
                    out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
                    // step6 输出文件
                    template.process(dataMap, out);
                    System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^"+str+"Repository.java 文件创建成功 !");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}